import { initData } from '@/api/backApi'

export default {
  data() {
    return {
      data: [], pageIndex: 1, pageSize: 50, total: 0, url: '', params: {}, query: {}, sizes: [50, 100] }
  },
  methods: {
    async init() {
      if (!await this.beforeInit()) {
        return
      }
      return new Promise((resolve, reject) => {
        // 弹出遮罩层
        initData(this.url, this.params).then(res => {
          if (res.data.list) {
            this.total = res.data.total
            this.data = res.data.list
          } else {
            this.data = res.data
          }
          resolve(res)
        }).catch(err => {
          // 关闭遮罩
          reject(err)
        })
      })
    },
    beforeInit() {
      return true
    },
    dleChangePage(size) {
      if (size === undefined) {
        size = 1
      }
      if (this.data.length === size && this.page !== 0) {
        this.page = this.page - 1
      }
    },
    toQuery() {
      this.pageIndex = 1
      this.init()
    },
    pageChange(e) {
      this.pageIndex = e
      this.init()
    },
    sizeChange(e) {
      this.pageIndex = 1
      this.pageSize = e
      this.init()
    }
  }
}
